//
//  WCProViewController.swift
//  iiatn
//
//  Created by mnine on 18/3/31.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import SVProgressHUD

let proCellID = "proCellID"
class WCProViewController: UITableViewController {

    /// 首页列表数据
    var items = [WPro]()
    var key: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.automaticallyAdjustsScrollViewInsets = false
        view.backgroundColor = WGlobalWhiteColor()
        setupTableView()
        // 添加刷新控件
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        // 被继承的东西－－－－
    }
    
    func loadData() {
        // 被继承需加载－－－－
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func setupTableView() {
        view.backgroundColor = WLightWhiteColor()
        SVProgressHUD.showProgress(0.5)
        tableView.rowHeight = 150
        // 分割线
        tableView.separatorStyle = .singleLine
        // top left bottom right
        tableView.contentInset = UIEdgeInsetsMake(kTitlesViewY, 0, 0, 0)
        tableView.scrollIndicatorInsets = tableView.contentInset
        /// 切入视图cell，并注册
        let nib = UINib(nibName: String(describing: WProViewCell.self), bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: proCellID)
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
        // return items.count
    }
    
    // 一共多少行
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return items.count
    }
    
    // 行数据定义
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: proCellID) as! WProViewCell
        cell.selectionStyle = .gray
        cell.proItem = items[indexPath.row]
        
        return cell
    }
    
    // MARK: - UITableViewDelegate
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if whaslogin!{
            let detailVC = WProDetailViewController()
            detailVC.proItem = items[indexPath.row]
            detailVC.title = " "
            navigationController?.pushViewController(detailVC, animated: true)
        }else{
            let alertController = UIAlertController(title: "会员专享",
                                                    message: "\n您还没有登录，是否前往登录？", preferredStyle: .alert)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let okAction = UIAlertAction(title: "确认", style: .default, handler: {
                action in
                self.tologin()
                
            })
            alertController.addAction(cancelAction)
            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)
        }
    }
    
    func tologin(){
        let loginVC = WLoginViewController()
        loginVC.title = "登录"
        let nav = WNavigationController(rootViewController: loginVC)
        present(nav, animated: true, completion: nil)
    }

}
